const app = getApp()
const ble = require("ble")

let get_settings = () => {
	let keys_num = Object.keys(app.data.common_store.settings).length
	if(keys_num == 0) {
		get_store_settings_syn()
	}
}

let get_store_settings_syn = () => {
	let s = wx.getStorageSync("settings")
	let settings_num = Object.keys(s).length
	
	if(settings_num == 0) {
		app.data.common_store.settings = app.data.common_store.settings_default
	}
	else {
		app.data.common_store.settings = s
	}
	save()
}

let set_item = (key, value) => {
	if(key != "callsign" && key != "play_my_code") {		
		ble.send("set:" + key + ":" + value)
	}
	app.data.common_store.settings[key] = value
	save()
}

let save = () => {
	// 存储在本地
	wx.setStorage({
		key: "settings",
		data: app.data.common_store.settings,
		success(res) {
			console.log("设置存储本地 成功")
		},
		fail(res) {
			console.log("设置存储本地 失败")
		}
	})
}



module.exports = {
	get_settings: get_settings,
	set_item: set_item,
}